#include "LoginPage.h"
#include "ui_LoginPage.h"
#include <QDebug>
LoginPage::LoginPage(QRect screenGeometry,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginPage)
{
    ui->setupUi(this);
    this->screenGeometry = screenGeometry;
    this->setFixedSize(this->screenGeometry.width(),this->screenGeometry.height());
//    //QLabel *pImageLabel = new QLabel(this);
//    pUserLineEdit = new QLineEdit(this);
//    pPasswordLineEdit = new QLineEdit(this);
//    pRememberCheckBox = new QCheckBox(this);
//    pAutoLoginCheckBox = new QCheckBox(this);
//    pLoginButton = new QPushButton(this);
//    pRegisterButton = new QPushButton(this);
//    pForgotButton = new QPushButton(this);

//    // 设置文本
//    pUserLineEdit->setPlaceholderText(QStringLiteral("手机"));
//    pPasswordLineEdit->setPlaceholderText(QStringLiteral("密码"));
//    pPasswordLineEdit->setEchoMode(QLineEdit::Password);
//    pRememberCheckBox->setText(QStringLiteral("记住密码"));
//    pAutoLoginCheckBox->setText(QStringLiteral("自动登录"));
//    pLoginButton->setText(QStringLiteral("登录"));
//    pRegisterButton->setText(QStringLiteral("注册账号"));
//    pForgotButton->setText(QStringLiteral("找回密码"));

//    pIpEdit =new QLineEdit("192.168.1.166");
//    pPortEdit = new QLineEdit("8059");
//    ipLabel= new QLabel("服务器ip");
//    portLabel=new QLabel("          端口");

    connect(this->ui->pRegisterButton,&QPushButton::clicked,this,&LoginPage::zhucePage);

    setMLayout();
}

LoginPage::~LoginPage()
{
    qDebug()<<"会进入析构函数"<<endl;
    delete ui;
}

QLineEdit *LoginPage::getUser()
{
    return this->ui->pUserLineEdit;
}

QLineEdit *LoginPage::getPass()
{
    return this->ui->pPasswordLineEdit;
}

void LoginPage::setServerStatus(bool r)
{
    if(r==true)
    {
        this->ui->status->setStyleSheet("background-color: rgb(0, 255, 0);");//连接上了
        this->ui->connect->setText("断开服务器");
    }
    else
    {
        this->ui->status->setStyleSheet("background-color: rgb(255, 0, 0);");//断开了
        this->ui->connect->setText("连接服务器");
    }
}

QString LoginPage::packToString(QString phone,QString pass)
{
    QString str("7E7E;kind:phone;function:01;phone:%1;pass:%2;");
    return str.arg(phone).arg(pass);
}


void LoginPage::zhucePage()
{
    if(this->zhuCe==nullptr)
    {
        this->zhuCe=new Zhuce(this->screenGeometry);
        connect(this->zhuCe,&Zhuce::sendServerData,this,&LoginPage::sendServerData);
        connect(this,&LoginPage::zhuCeStatus,this->zhuCe,&Zhuce::zhuCeStatus);
    }
    this->zhuCe->exec();
}

void LoginPage::setMLayout()
{
//    this->logWidget=new QWidget(this);
//    this->logWidget->setGeometry(0,40,this->screenGeometry.width(),240);

//    pLayout = new QGridLayout(this);

//    pLayout->addWidget(ipLabel,0,0,1,1);
//    pLayout->addWidget(pIpEdit,0,1,1,2);
//    pLayout->addWidget(portLabel,0,3,1,1);
//    pLayout->addWidget(pPortEdit,0,4,1,1, Qt::AlignRight);


//        pLayout->addWidget(pUserLineEdit, 1, 0, 1, 4);
//        pLayout->addWidget(pRegisterButton, 1, 4,1,1);
//        // 密码输入框 第1行，第1列开始，占1行2列
//        pLayout->addWidget(pPasswordLineEdit, 2, 0, 1, 4);
//        pLayout->addWidget(pForgotButton, 2, 4,1,1);
//        // 记住密码 第2行，第1列开始，占1行1列 水平居左 垂直居中
//        pLayout->addWidget(pRememberCheckBox, 3, 1, 1, 1, Qt::AlignLeft | Qt::AlignVCenter);
//        // 自动登录 第2行，第2列开始，占1行1列 水平居右 垂直居中
//        pLayout->addWidget(pAutoLoginCheckBox, 3, 3, 1, 1, Qt::AlignRight | Qt::AlignVCenter);
//        // 登录按钮 第3行，第1列开始，占1行2列
//        pLayout->addWidget(pLoginButton, 4, 0, 1, 5);

//    // 设置水平间距
//    pLayout->setHorizontalSpacing(10);
//    // 设置垂直间距
//    pLayout->setVerticalSpacing(10);
//    // 设置外间距
//    pLayout->setContentsMargins(10, 10, 10, 10);
//    this->logWidget->setLayout(pLayout);
}

void LoginPage::on_pLoginButton_clicked()
{//登陆
    QString str=packToString(this->ui->pUserLineEdit->text(),this->ui->pPasswordLineEdit->text());
    emit sendServerData(str);
}

void LoginPage::on_connect_clicked()
{qDebug()<<"连接服务器"<<endl;
    if(this->ui->connect->text()=="连接服务器")
        emit connectServer(this->ui->pUserLineEdit->text(),this->ui->pPasswordLineEdit->text(),this->ui->pIpEdit->text(),this->ui->pPortEdit->text());
    else
    {
        emit disConnectServer();
        setServerStatus(false);
    }
}

